##=============================================================================
## Appendix Figure 7
##=============================================================================

##-----------------
# clear environment
rm(list=ls())
options(stringsAsFactors = FALSE, scipen = 999)
# source("R/functions.R")

seed <- sample.int(.Machine$integer.max, 1)
set.seed(seed)

ipak <- function(pkg){new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if(length(new.pkg)) install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}

packages <- c("tidyverse", "hrbrthemes")

ipak(packages)

##---------
# Load data
#setwd("/Users/austinknuppe/Library/CloudStorage/Dropbox/Ukraine2022WartimeSurvey/Paper_peace/final_version_oct_2024/replication-scripts")
load("clean_ukraine_data.RData")

num_col <- function(x){
  any(!is.character(x))
}

pers_1 <- raw_dat %>% 
  janitor::clean_names() %>%
  haven::zap_label() %>% 
  mutate_if(is.character, list(~na_if(., ""))) %>% 
  mutate_if(num_col, as.numeric) %>% 
  dplyr::select(v1, v141_1:v141_8) %>% 
  mutate(across(starts_with("v141_"), ~ifelse(.x == 1, 1, 0))) %>% 
  mutate(across(starts_with("v141_"), factor, levels = 0:1, 
                labels = c("No", "Yes"))) %>% 
  mutate(wave = 1) %>% 
  as.data.frame() %>% 
  rename(id = v1, 
         pers_1 = v141_1, 
         pers_2 = v141_2, 
         pers_3 = v141_3,
         pers_4 = v141_4, 
         pers_5 = v141_5, 
         pers_6 = v141_6, 
         pers_dk = v141_7,
         pers_ref = v141_8)

pers_2 <- raw_dat2 %>% 
  janitor::clean_names() %>%
  haven::zap_label() %>% 
  mutate_if(is.character, list(~na_if(., ""))) %>% 
  mutate_if(num_col, as.numeric) %>% 
  dplyr::select(v1, v203:v210) %>% 
  rename(id = v1) %>% 
  mutate(across(starts_with("v"), ~ifelse(.x == 1, 1, 0))) %>% 
  mutate(across(starts_with("v"), factor, levels = 0:1, 
                labels = c("No", "Yes"))) %>% 
  mutate(wave = 2) %>% 
  as.data.frame() %>% 
  rename(pers_1 = v203, 
         pers_2 = v204, 
         pers_3 = v205,
         pers_4 = v206, 
         pers_5 = v207, 
         pers_6 = v208, 
         pers_dk = v209,
         pers_ref = v210)

pers <- bind_rows(pers_1, pers_2) %>% 
  mutate(wave = factor(wave, levels = 1:2, labels = c("Wave 1", "Wave 2"))) %>% 
  as.data.frame()

rm(pers_1, pers_2)

pers <- pers %>% 
  as.data.frame() %>% 
  rename(
    "Services disrupted (electricity provision, water provision, garbage collection, etc.)" = pers_1,
    "Missile strikes or shelling of your city" = pers_2,
    "Missile strikes or shelling of your neighborhood" = pers_3,
    "Missile strikes or shelling of your home" = pers_4,
    "Looting or stealing of food or material goods" = pers_5,
    "Violence or other direct harm from Russian soldiers (shooting, beating, detention, etc.)" = pers_6,
    "Don't Know" = pers_dk,
    "Refused" = pers_ref)

mylikert <- likert::likert(pers[, 2:7], grouping = pers$wave)

plot(mylikert, colors = c("gray40", "gray20")) +
  scale_y_continuous(labels = likert:::abs_formatter, lim = c(-100, 100),
                     breaks = seq(-100, 100, 25)) +
  theme(text = element_text(size = 16))


rm(list = ls())
##=============================================================================
## End of File
##=============================================================================